<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use Exception;
use think\Db;
use think\response\Json;

class Article extends Backend
{
    protected $model = null;
    protected $categoryModel = null;

    // 无需鉴权的方法
    protected $noNeedRight = ['index', 'recyclebin'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Article;
        $this->categoryModel = new \app\common\model\ArticleCategory;
    }

    /**
     * 查看
     *
     * @return string|Json
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $total = $this->model->where($where)->count();

            $result = [
                "total" => $total,
                "rows" => $list,
            ];
            return json($result);
        }

        $this->assignconfig('categoryList', $this->categoryModel->select());
        return $this->view->fetch();
    }

    /**
     * 添加
     *
     * @return string|Json
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                $result = false;
                Db::startTrans();
                try {
                    $result = $this->model->allowField(true)->save($params);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $categoryList = $this->categoryModel->select();
        $this->view->assign('categoryList', $categoryList);
        return $this->view->fetch();
    }

    /**
     * 编辑
     *
     * @param $ids
     * @return string|Json
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $categoryList = $this->categoryModel->select();
        $this->view->assign([
            'categoryList' => $categoryList,
            'row' => $row,
        ]);
        return $this->view->fetch();
    }
}
